﻿<Window x:Class="SwiftTailer.Wpf.Pages.AdHocTailingWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SwiftTailer.Wpf.Pages"
        xmlns:viewModels="clr-namespace:SwiftTailer.Wpf.ViewModels"
        xmlns:wpf="clr-namespace:SwiftTailer.Wpf"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:converters="clr-namespace:SwiftTailer.Wpf.Converters"
        xmlns:commands="clr-namespace:SwiftTailer.Wpf.Commands"
        mc:Ignorable="d"
        Title="{Binding WindowTitle}" Height="800" Width="600">
    
    <Window.DataContext>
        <viewModels:AdHocTailingViewModel />
    </Window.DataContext>

    <Window.Resources>
        <converters:InverseBoolConverter x:Key="InverseBoolConverter" />
        <converters:HighlightConverter x:Key="HighlightConverter"/>
        <converters:LineCountDisplayConverter x:Key="LineCountDisplayConverter" />
        <converters:EnumBoolConverter x:Key="EnumBoolConverter" />
        <converters:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter" />
        <Style TargetType="ListBoxItem" x:Key="LogLineStyle">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="View Selected" Command="commands:StaticCommands.OpenLogLineCommand" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget}" />
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_DoubleClick" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="Find">
                    <Setter Property="Background" Value="Yellow" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="Error">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="General">
                    <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Highlight, Converter={StaticResource HighlightConverter}}" Value="Hide">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="StatusBarItem">
            <Setter Property="Margin" Value="10 0"/>
        </Style>
        <Style TargetType="Button" x:Key="TransportControlButtons">
            <Setter Property="Width" Value="25" />
            <Setter Property="Height" Value="25"/>
            <Setter Property="Margin" Value="2 0" />
        </Style>
    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <ToolBarTray>
            <ToolBar ToolTip="Transport">
                <Button ToolTip="Start tailing" Style="{StaticResource TransportControlButtons}"
                        Command="{Binding StartTailingCommand}">
                    <Image Source="../Assets/playback_play.png" />
                </Button>
                <Button ToolTip="Pause tailing" Style="{StaticResource TransportControlButtons}"
                        Command="{Binding StopTailingCommand}">
                    <Image Source="../Assets/playback_pause.png" />
                </Button>
            </ToolBar>
        </ToolBarTray>
        
        <!-- LOG LINES -->
        <ListBox x:Name="LogList" ItemsSource="{Binding TailFile.LogLines, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" SelectionMode="Extended" 
                                 ItemContainerStyle="{StaticResource LogLineStyle}" FontFamily="{x:Static wpf:Settings.LogWindowFontFamily}"
                                 SelectedIndex="{Binding TailFile.SelectedLine}" SelectionChanged="LogList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Content}" ToolTip="{Binding LineContext}" 
                                                   ToolTipService.InitialShowDelay="1000" ToolTipService.ShowDuration="20000" ToolTipService.HasDropShadow="True"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Right" Margin="5 5 5 0">
            <CheckBox Content="Follow Tail" IsChecked="{Binding TailFile.FollowTail}" />
        </StackPanel>

        <!-- SEARCH PANEL -->
        <Expander Grid.Row="3" Margin="5 10 2 5" Style="{DynamicResource ExpanderStretchHeader}">
            <Expander.Header>
                <DockPanel LastChildFill="True">
                    <Label Content="Search:"/>
                    <TextBox x:Name="LogFilter" HorizontalAlignment="Stretch" BorderBrush="Gray" VerticalContentAlignment="Center"
                                        Text="{Binding TailFile.SearchOptions.SearchPhrase, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=500}"/>
                </DockPanel>
            </Expander.Header>
            <StackPanel Orientation="Horizontal">
                <GroupBox Header="Case Sensitivity" Padding="5 5 5 0" Margin="2"
                                          ToolTip="Whether or not the search will use Ordinal matching rules.">
                    <StackPanel Orientation="Vertical">
                        <RadioButton GroupName="CaseSensitivity" Content="On" IsChecked="{Binding TailFile.SearchOptions.CaseSensitive, Mode=TwoWay}"/>
                        <RadioButton GroupName="CaseSensitivity" Content="Off" IsChecked="{Binding TailFile.SearchOptions.CaseSensitive, Converter={StaticResource InverseBoolConverter}, Mode=TwoWay}" />
                    </StackPanel>
                </GroupBox>

                <GroupBox Header="Search Mode" Padding="5 5 5 0" Margin="2"
                                          ToolTip="How the search will return hits. Find will highlight hits, Filter will disclude all non-hits.">
                    <StackPanel Orientation="Vertical">
                        <RadioButton GroupName="SearchMode" Name="FindModeRdo"  Content="Find" IsChecked="{Binding TailFile.SearchOptions.SearchMode, Mode=TwoWay, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Find}" />
                        <RadioButton GroupName="SearchMode" Name="FilterModeRdo" Content="Filter" IsChecked="{Binding TailFile.SearchOptions.SearchMode, Mode=TwoWay, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Filter}"/>
                    </StackPanel>
                </GroupBox>

                <GroupBox Header="Lines of Context" Visibility="{Binding ElementName=FilterModeRdo, Path=IsChecked, Converter={StaticResource BooleanVisibilityConverter}}"
                                          ToolTip="Experimental. Each line will contain a tool tip with the filter hit along with x lines of context, before and/or after.">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="Head"/>
                        <xctk:IntegerUpDown Value="{Binding TailFile.SearchOptions.ContextHeadSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=1000}" Minimum="0" />
                        <TextBlock Text="Tail"/>
                        <xctk:IntegerUpDown Value="{Binding TailFile.SearchOptions.ContextTailSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=1000}" Minimum="0" />
                    </StackPanel>
                </GroupBox>

                <GroupBox Header="Phrase Type" Padding="5 5 5 0" Margin="2"
                                          ToolTip="How the search phrase will be treated">
                    <StackPanel Orientation="Vertical">
                        <RadioButton GroupName="PhraseType" Content="Literal" IsChecked="{Binding TailFile.SearchOptions.PhraseType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Literal, Mode=TwoWay}" />
                        <RadioButton GroupName="PhraseType" Content="Regex" IsChecked="{Binding TailFile.SearchOptions.PhraseType, Converter={StaticResource EnumBoolConverter}, ConverterParameter=Regex, Mode=TwoWay}"/>
                    </StackPanel>
                </GroupBox>

                <GroupBox Header="Error keywords" Padding="5 5 5 0" Margin="2" Width="150" Height="100"
                                          ToolTip="Comma delimited set of words that will be highlighted in red">
                    <Grid>
                        <TextBox Text="{Binding TailFile.SearchOptions.ErrorPhrases, Mode=TwoWay, Delay=500, UpdateSourceTrigger=PropertyChanged}" />
                    </Grid>
                </GroupBox>

                <GroupBox Header="General keywords" Padding="5 5 5 0" Margin="2" Width="150" Height="100"
                                          ToolTip="Comma delimited set of words that will be highlighted in green">
                    <Grid>
                        <TextBox Text="{Binding TailFile.SearchOptions.GeneralPhrases, Mode=TwoWay, Delay=500, UpdateSourceTrigger=PropertyChanged}" />
                    </Grid>
                </GroupBox>
            </StackPanel>
        </Expander>

        <!-- STATUS BAR -->
        <StatusBar Grid.Row="4" MinHeight="20">
            <StatusBarItem Content="{Binding Status, FallbackValue=Idle}" HorizontalAlignment="Left" />
            <StatusBarItem Content="{Binding TailFile.Name, FallbackValue='myfile.txt'}" HorizontalAlignment="Left" />
            <StatusBarItem Content="{Binding TailFile.LineCount, Converter={StaticResource LineCountDisplayConverter}}" />
        </StatusBar>
    </Grid>
</Window>
